package org.beetl.json.loc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class IndexLocationList {
	Set<IndexLocation> set = new HashSet<IndexLocation>();
	
	public void addIndexLocation(IndexLocation l){
			set.add(l);
		
	}
	
	public List<IndexLocation> match(Object key){
		Iterator<IndexLocation> it = set.iterator();
		List list = new ArrayList<IndexLocation>();
		while(it.hasNext()){
			IndexLocation l = it.next();
			if(l.getRef().equals("*")){
				if(l.getAction()!=null){
					list.add(l);
				}
				
			}
			if(l.getRef().equals(key)){
				if(l.getAction()!=null){
					list.add(l);
				}
				
			}
		}
		return list;
	}
	
	public String  getMatchPolicy(Object key){
		StringBuilder sb = new StringBuilder();
		Iterator<IndexLocation> it = set.iterator();
		while(it.hasNext()){
			IndexLocation l = it.next();
			if(l.getRef().equals("*")){
				if(l.getAction()==null)
				sb.append(l.getPolicy()).append(",");
			}
			if(l.getRef().equals(key)){
				if(l.getAction()==null)
				sb.append(l.getPolicy()).append(",");
			}
		}
		if(sb.length()!=0){
			sb.setLength(sb.length()-1);
			return sb.toString();
		}else{
			return null;
		}
		
	}
}
